<script src="/js/private/admin/roles/index.js" type="text/javascript"></script> 
<script>
    common.setMainTitle('Quản trị vai trò');
</script>
<form id="mainform" name=frm method=post action="/admin/roles/input">
    <table style="margin-top: 10px" border="0" width="100%" cellpadding="0" cellspacing="0" id="product-table">
        <thead>
            <tr>
                <th width="5%" class="table-header-stt" style="text-align: center">STT</th>
                <th width="5%" class="table-header-repeat line-left" style="text-align: center">
                    <input type=checkbox name=DELALL onclick="SelectAll(this,'DEL')">
                </th>
                <th width="10%" class="table-header-repeat line-left" style="text-align: left"><a>Tên</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a><a>Mã vai trò</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a><a>Trạng thái</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a><a>Điều hướng</a></th>
                <th width="" class="table-header-repeat line-left" style="text-align: left"><a><a>Phân quyền</a></th>
            </tr>
        </thead>
        <tfoot>
        </tfoot>
        <tbody>
        <script>
            var arrNav=new Array();
        </script>
            <?php
            if(count($this->data)==0){
                echo "<tr><td colspan=6>Không có dữ liệu nào được tìm thấy</td></tr>";
            } else {
                $stt = 1;
            }
            $arrStatus = array(
                0 => 'Chưa kích hoạt',
                1 => 'Đã kích hoạt'
            );
            $tt = 0;
            //var_dump($this->data);exit;
            foreach($this->data as $item){?>
            <tr style="cursor:pointer">
                <td style="text-align: center"><?=$stt?></td>
                <td style="text-align: center">
                    <input type="checkbox" name="DEL[]" value="<?=$item['CODE']?>" />
                </td>
                <td>
                    <a onClick="ItemClick('<?php echo $item['CODE'] ?>')">
                        <?=$item['NAME'];?>
                    </a>
                </td>
                <td>
                    <a onClick="ItemClick('<?php echo $item['CODE'] ?>')">
                        <?=$item['CODE'];?>
                    </a>
                </td>
                <td>
                    <a>
                        <?=$arrStatus[$item['STATUS']];?>
                    </a>
                </td>
                <td>
                    <select id="<?php echo 'box'.$tt ?>" name="<?php echo 'box'.$tt ?>" value="" onchange="changeNavigation(this.value,'<?php echo $item['CODE'] ?>')">
                   <?php
                  
                    $navigation = '';
                    $tab = "&nbsp;&nbsp;&nbsp;&nbsp;";
                    foreach($this->dataSources as $item1){
                        if($item1['description'] !=""){
                            $keys = array_keys($item1['controller']);
                            $navigation .= '<optgroup label="MODULE '.strtoupper($item1['description']).'" style="color:#B8B8B8;font-style:normal">';
                            for($i=0;$i<count($keys);$i++)
                            {
                                $description = $item1['controller'][$keys[$i]]['description'];
                                
                                $des = '';
                                if(strtoupper($description) != strtoupper($keys[$i])){
                                    $des = ' : '.$description;
                                }
                                
                                $navigation .= '<optgroup label="'.$tab.strtolower($keys[$i]).$des.'" value="'.$keys[$i].'" style="color:#004FA0;font-style:normal">';
                                $arr_action = $item1['controller'][$keys[$i]]['action'];
                                $keyAction = array_keys($arr_action);
                                for($ii=0;$ii<count($keyAction);$ii++)
                                {
                                    $selected = "";
                                    $value = $item1['description'].'/'.$keys[$i].'/'.$keyAction[$ii];
                                    if($item['NAVIGATION'] == $value){
                                        $selected = "selected ='true' ";
                                    }
                                    $desAction = '';
                                    if(strtoupper($arr_action[$keyAction[$ii]]) != strtoupper($keyAction[$ii])){
                                        $desAction = ' : '.$arr_action[$keyAction[$ii]];
                                    }
                                    $navigation .= '<option value="'.$value.'" name="'.$item1['description'].'_'.$keys[$i].'_'.$keyAction[$ii].'" style="color:#393939;" '.$selected.' >'.$tab.$tab.$tab.$keyAction[$ii].$desAction.'</option>';
                                }
                                $navigation .= "</optgroup>";
                            }
                            $navigation .= "</optgroup>";
                        }
                    }
                   echo $navigation;
                   ?>
                   </select>
                   <script>
                        
                   </script>
                </td>
                <td>
                    <a href="javascript:common.loadAjaxPushState('/admin/authorization/index/code/<?=$item['CODE']?>')">
                        Phân quyền
                    </a>
                </td>
            </tr>
            <?php 
            $stt++;
            $tt++;
            }
            ?>
        </tbody>
    </table>
</form>
<script>
    function changeNavigation(value,code){
        $cnf = confirm('BẠN CÓ CHẮC CHẮN KHÔNG ? ');
        if($cnf === true){
            var jxNav = new Jx();
            jxNav.setParameter('URLNAV',value);
            jxNav.setParameter('CODENAV',code);
            jxNav.success('completeChangeNavigation');
            jxNav.request('/admin/roles/changenavigation','POST');
        }
    }
    function completeChangeNavigation(){
        if(rsChangeNav == 0){
            alert("Thay đổi thất bại \n\n" + rsChangeNavError);
        }
    }
    function ItemClick(code)
    {
        common.loadAjaxPushState('/admin/roles/input/CODE/'+code)
    }
    function add(){
        common.loadAjaxPushState('/admin/roles/input');
    }
    function SelectAll(selobj,childobj){
         var arr = document.getElementsByName(childobj+"[]");
         for(var i=0;i<arr.length;i++){
          arr[i].checked = selobj.checked;
         }
    }
    function del(){
        var ln = 0;
        var arr = document.getElementsByName('DEL[]');
        for(var i = 0 ; i < arr.length ;i++ )
        {
            if(arr[i].checked == true)
            {
                ln = 1;
                break;
            }
        }
        if(ln == 1)
        {
            if(confirm("Bạn có muốn xóa không")){
                document.frm.action="/admin/roles/delete";
                document.frm.method="post";
                document.frm.submit();
                //common.submitForm(document.frm,'sucess');
                //common.loadAjaxPushState('/admin/roles/index');
            }
        }
        else
        {
            alert("Bạn phải một dòng chọn để xóa");
        }
    }
</script>